package com.open.pokergames.base.activity

import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import com.king.app.dialog.AppDialog
import com.king.app.dialog.AppDialogConfig
import com.king.app.updater.AppUpdater
import com.open.pokergames.NetConfig
import com.open.pokergames.R
import com.open.pokergames.comm.repository.cache.LocalCache
import com.open.pokergames.comm.repository.entity.AppVersionEntity
import com.open.pokergames.comm.repository.entity.LoginDto
import com.open.pokergames.comm.repository.entity.ResultInfo
import com.open.pokergames.comm.repository.viewModel.SystemViewModel
import com.open.pokergames.comm.userCenter.LoginActivity
import com.sinothk.android.utils.XUtils
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

@Deprecated("")
abstract class MainBaseActivity : MessageBaseActivity() {

    var viewModel: SystemViewModel? = null

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(false)
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

    abstract fun initTab()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main_tabs)
        viewModel = SystemViewModel()

        initTab()
        refreshInfo()
        initNetData()
    }

    private fun initNetData() {
        if (!XUtils.net().isConnected) {
            return
        }
        checkNewVersion()
    }

    private fun checkNewVersion() {
        viewModel?.lastVersion("MAIN")
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun lastVersionEventBusCallback(resultInfo: ResultInfo<AppVersionEntity>) {
        if (resultInfo.eventType != "MAIN") {
            return
        }

        when (resultInfo.code) {
            ResultInfo.SUCCESS -> {
                if (resultInfo.data == null) {
                    return
                }

                val appVersion = resultInfo.data

                if (resultInfo.data.verCode > XUtils.app().appVersionCode) {
                    //简单弹框升级
                    val config = AppDialogConfig()
                    config.setTitle("新版本升级")
                        .setOk("升级")
                        .setContent(XUtils.string().getNotNullValue(appVersion.updateContent)).onClickOk = View.OnClickListener {
                        if (XUtils.string().isNotEmpty(appVersion.diskPath)) {
                            AppUpdater.Builder()
                                .serUrl(NetConfig.getAppUrl(appVersion.diskPath))
                                .build(this)
                                .start()
                            XUtils.toast().show("正在通知栏中下载...")
                        } else {
                            XUtils.toast().show("地址不可用")
                        }
                        AppDialog.INSTANCE.dismissDialog()
                    }
                    AppDialog.INSTANCE.showDialog(this@MainBaseActivity, config)
                }
                LocalCache.setAppVersion(appVersion)
            }
            ResultInfo.TOKEN_OVERDUE -> {
            }
            else -> {
            }
        }
    }

    public fun refreshInfo() {
        viewModel!!.loginAuto()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(resultInfo: ResultInfo<LoginDto>) {
        if (resultInfo.eventType != "loginAuto") {
            return
        }

        when (resultInfo.code) {
            ResultInfo.SUCCESS -> {
                LocalCache.setLoginUser(resultInfo.data)
                LocalCache.setAutoLogin(true)
                updateHomeTop()
            }
            else -> {
                XUtils.toast().show(resultInfo.msg)
                LocalCache.logout()
                XUtils.page().finishAll()
                XUtils.intent().openActivity(this, LoginActivity::class.java)
                    .start()
            }
        }
    }

    abstract fun updateHomeTop()
}